﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace formworks.DBO.Core
{
    public class DBSessionFactory
    {
        public string connectionString { get; set; }
        public System.Data.SqlClient.SqlConnection connection { get; set; }
        public string DBVersion { get; set; }

        public DBSessionFactory()
        {
            
        }

        public DBSessionFactory(string connectionString)
        {
            this.connectionString = connectionString;
        }

        public DBSessionFactory(SqlConnection connection)
        {
            this.connection = connection;
        }

        /// <summary>
        /// 打开一个数据库会话
        /// </summary>
        /// <returns></returns>
        public virtual DBSession openSession()
        {
            try
            {

                //判断连接是否建立
                if (null == connection)
                {
                    //没有建立就建立连接对象
                    connection = new SqlConnection(connectionString);
                }

                //创建一个数据库对话对象
                DBSession dbSession = new DBSession(connection);
                dbSession.DBVersion = DBVersion;
                return dbSession;
            }
            catch (System.Exception ex)
            {
                if (null != connection)
                {
                    if (connection.State != ConnectionState.Closed)
                    {
                        connection.Close();
                    }//
                }//end con
                throw ex;
            }
        }

    }//endclass
}//endnamespace
